package tt.dz.egobus.servicemanager.service.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.egobus.base.dao.BaseDao;
import tt.dz.egobus.servicemanager.base.service.AbstractExtendService;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.dao.BusRegisterServiceDao;
import tt.dz.egobus.servicemanager.entities.BusRegisterService;
import tt.dz.egobus.servicemanager.entities.BusService;
import tt.dz.egobus.servicemanager.service.BusRegisterServiceService;
import tt.dz.egobus.servicemanager.service.BusServiceService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.BUS_EXIST_RELATIVE;

/**
 * @author Anson
 * @date 17-3-14
 * @name
 * @since 1.0.1
 */
@Service("busRegisterServiceServiceImpl")
public class BusRegisterServiceServiceImpl extends AbstractExtendService<BusRegisterService, String>
        implements BusRegisterServiceService {

    @Autowired
    @Qualifier("busRegisterServiceDaoImpl")
    private BusRegisterServiceDao dao;

    @Autowired
    @Qualifier("busServiceServiceImpl")
    private BusServiceService busServiceService;

    @Override
    protected BaseDao<BusRegisterService, String> getSelfDao() {
        return this.dao;
    }

    @Transactional
    @Override
    public void updateIsValidateStatus(boolean isValidate, String busId, String serviceId) {
        Map<String, Object> params = new HashMap<String, Object>();
        StringBuilder hql =
                new StringBuilder(" update  BusRegisterService set isValidate =:isValidate where 1=1  ");
        params.put("isValidate", isValidate);
        if (!StringUtils.isEmpty(busId)) {
            hql.append(" and busId=:busId ");
            params.put("busId", busId);
        }
        if (!StringUtils.isEmpty(serviceId)) {
            hql.append(" and busServiceId=:busServiceId ");
            params.put("busServiceId", serviceId);
        }
        this.dao.batch(hql.toString(), params);
    }

    @Override
    @Transactional
    public int[] queryBusinessCodeByBusId(String busId) {
        if (StringUtils.isEmpty(busId)) {
            return new int[0];
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eq#isValidate", true);
        params.put("eq#busId", busId);
        List<BusRegisterService> list = this.dao.list(params);
        int[] codes = new int[0];
        if (!CollectionUtils.isEmpty(list)) {
            codes = new int[list.size()];
            BusService busService = null;
            int i = 0;
            for (BusRegisterService service : list) {
                busService = service.getBusService();
                if (null == busService) {
                    continue;
                }
                codes[i++] = busService.getBusinessCode();
            }
        }
        return codes;
    }

    @Override
    @Transactional
    public double getServiceFeeByBusIdBusinessCode(String busId, int businessCode) {
        if (StringUtils.isEmpty(busId)) {
            return 0D;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("businessCode", businessCode);
        params.put("busId", busId);
        params.put("isValidate", true);
        String hql = " select t.busService.basicsCost from BusRegisterService t where  t.busId=:busId " +
                "  and t.isValidate=:isValidate " +
                "  and t.busService.businessCode=:businessCode  ";
        List<Double> list = (List<Double>) this.dao.list(hql, params, 0, 0, null);
        return list == null || list.isEmpty() ? 0D : list.get(0).doubleValue();
    }

    @Override
    @Transactional
    public List<tt.dz.egobus.servicemanager.entities.BusService> queryBusAbleBindServices(String busId) {
        List<tt.dz.egobus.servicemanager.entities.BusService> list
                = new ArrayList<tt.dz.egobus.servicemanager.entities.BusService>();
        if (StringUtils.isEmpty(busId)) {
            return list;
        }
        //当前hql语句还可以优化
        String hql = " from BusService t  where   t.isActive=true  ";
        hql += " and  t.id not in " +
                "    ( select a.busServiceId from BusRegisterService a  " +
                "       where  a.busId=:busId    " +
                "   ) ";

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("busId", busId);
        list = (List<tt.dz.egobus.servicemanager.entities.BusService>)
                busServiceService.list(hql, params, null);
        return list;
    }

    @Override
    @Transactional
    public int deleteRelativeByBusId(String busId) {
        if (StringUtils.isBlank(busId)) {
            return 0;
        }
        return this.dao.deleteRelativeByBusId(busId);
    }

    @Override
    public void validateServiceRelative(String busId, boolean isFilterValidate) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eq#busId", busId);
        if (isFilterValidate) {
            params.put("eq#isValidate", true);
        }
        int count = this.dao.count(params);
        if (count > 0) {
            throw new BusinessException(BUS_EXIST_RELATIVE.getCodeStatus());
        }
    }
}
